const { Sequelize, DataTypes } = require('sequelize');
const sequelize = require('../config/db');

const Order = sequelize.define('Order', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  order_no: {
    type: DataTypes.STRING(50),
    unique: true,
    allowNull: false
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  shop_id: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  total_amount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false
  },
  pay_amount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false
  },
  discount_amount: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0
  },
  delivery_fee: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0
  },
  order_type: {
    type: DataTypes.ENUM('dine_in', 'delivery'),
    allowNull: false
  },
  table_no: {
    type: DataTypes.STRING(20)
  },
  take_time: {
    type: DataTypes.DATE
  },
  address_id: {
    type: DataTypes.INTEGER
  },
  status: {
    type: DataTypes.ENUM('pending', 'paid', 'preparing', 'ready', 'completed', 'cancelled'),
    defaultValue: 'pending'
  },
  pay_type: {
    type: DataTypes.ENUM('wechat', 'alipay', 'cash'),
    defaultValue: 'wechat'
  },
  pay_time: {
    type: DataTypes.DATE
  },
  remark: {
    type: DataTypes.TEXT
  }
}, {
  tableName: 'orders',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

module.exports = Order;